<template>
  <ContentWrap>
    <template #header>
      <div class="card-header">
        <span class="card-title">资源分类统计</span>
      </div>
    </template>
    <div class="card-content">
      <div class="chart" ref="chartRef"></div>
    </div>
  </ContentWrap>
</template>

<script lang="ts" setup>
import * as echarts from 'echarts'
import { ReportApi } from '@/api/dr/report'
const chartRef = shallowRef()
const myChart = shallowRef()
const dataList = ref<any>([
  // {
  //   id: '1',
  //   name: '青铜器',
  //   value: 62
  // },{
  //   id: '2',
  //   name: '瓷器',
  //   value: 232
  // },{
  //   id: '3',
  //   name: '陶器',
  //   value: 162
  // },{
  //   id: '4',
  //   name: '书画',
  //   value: 102
  // },{
  //   id: '5',
  //   name: '绘画',
  //   value: 261
  // },{
  //   id: '6',
  //   name: '玉石器',
  //   value: 49
  // },{
  //   id: '7',
  //   name: '石刻',
  //   value: 49
  // }
])
const initChart = () => {
  myChart.value = echarts.init(chartRef.value)
  const dataValues = dataList.value.map((v:any) => v.value)
  // 求和
  const dataTotalValue = dataValues.reduce((acc:any, cur:any) => acc + cur, 0)
  // 计算最大值
  const maxValue = Math.max(...dataValues)
  // 设置圆柱体高度
  const roundedMaxValue = Math.ceil(maxValue/100)*100
  // console.log('dataValues', maxValue, dataValues)
  // console.log('roundedMaxValue', roundedMaxValue)
  // 背景柱状图的数据
  const maxData = new Array(dataList.value.length).fill(roundedMaxValue)

  const labelSetting = {
    show: true,
    position: 'top',
    offset: [0, -10],
    // formatter: (param:any) => {
    //   return ((param.value / dataTotalValue) * 100).toFixed(0) + '%'
    // },
    fontSize: 18,
    fontFamily: 'Arial'
  }
  const option =  {
    // 移动柱状图
    type: 'move',
    // 移动数量
    moveNum: 6,
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: (params:any[]) => {
        const param = params.filter(item => item.seriesName === '内部柱子')[0]
        if(param) {
          return `${param.axisValueLabel}:${param.value}`
        }
      }
    },
    grid: {
      left: 60,
      right: 60,
      top: 50,
      bottom: 40
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      // 分割线
      splitLine: { show: false },
      // 坐标轴线
      axisLine: { show: false },
      // 刻度线
      axisTick: { show: false },
      splitArea: {
        interval: 0,
        show: true
      },
      data: dataList.value.map(v => ({
        value: v.name,
        textStyle: {
          align: 'center'
        }
      }))
    },
    yAxis: {
      type: 'value',
      nameTextStyle: {
        color: '#ffffff',
        fontSize: 22,
      },
      // 分割线
      splitLine: { 
        show: true,
        lineStyle: {
          color: '#ffffff',
          type: 'dashed' 
        }
      },
      // 坐标轴线
      axisLine: { show: false },
      // 刻度线
      axisTick: { show: false },
      axisLabel: {
        show: true,
        textStyle: {
          color: '#ffffff',
          fontSize: 21,
        },
      }
    },
    series: [
      {
        name: '上部圆',
        type: 'pictorialBar',
        silent: true, // 不显示提示消息
        symbolSize: [55,10],
        symbolOffset: [0,-6],
        symbolPosition: 'end',
        z: 12,
        color: '#ffe7da',
        data: maxData
      },
      {
        name: '背景圆柱',
        type: 'bar',
        barWidth: 55,
        barGap: '10%',
        itemStyle: {
          normal: {
          //   color: new echarts.graphic.LinearGradient(0,0,1,1, [
          //     {
          //       offset: 0,
          //       color: 'rgba(84,165,255,0.31)'
          //     },
          //     {
          //       offset: 0.55,
          //       color: 'rgba(0,115,255,0.14)'
          //     },
          //     {
          //       offset: 1,
          //       color: 'rgba(4,4,26,0)'
          //     }
          //   ])
            color: '#fff1e9',
          },
          // opacity: 0.8
        },
        data: maxData
      },
      {
        name: '内部柱子',
        type: 'bar',
        barWidth: 55,
        z: 12,
        barGap: '-100%',
        itemStyle: {
          // color: (params: any) => {
          //   return new echarts.graphic.LinearGradient(0,0,0,1, [
          //     {
          //       offset: 0,
          //       color: '#0E60B2'
          //     },
          //     {
          //       offset: 1,
          //       color: '#1FAEDE'
          //     }
          //   ], false)
          // },
          // opacity: 0.7
          color: '#ffb487',
        },
        data: dataValues
      },
      {
        name: '内部柱子上部圆',
        type: 'pictorialBar',
        silent: true, // 不显示提示消息
        symbolSize: [55,10],
        symbolOffset: [0,-6],
        symbolPosition: 'end',
        z: 16,
        color: '#ff8c45',
        label: labelSetting,
        data: dataValues
      },
      {
        name: '内部柱子底部圆',
        type: 'pictorialBar',
        silent: true, // 不显示提示消息
        symbolSize: [55,10],
        symbolOffset: [0,6],
        z: 12,
        color: '#ffb487',
        data: dataValues
      },
    ],
  }
  myChart.value.setOption(option)
  window.addEventListener('resize', () => {
    myChart.value.resize()
  })
}
const getData = () => {
  ReportApi.getIndexReport().then((res) => {
    // console.log('getIndexReport', res)
    const list = res.sourceTypeDataRespVOS || []
    dataList.value = list.map((item: any) => ({
      value: item.dataNum,
      name: item.dataName,
      count: item.dataCount
    }))
    initChart() 
  })
}
onMounted(() => {
  getData()
})
</script>

<style lang="scss" scoped>
.card-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .card-title {
    font-weight: bold;
    line-height: 32px;
  }
  .more {
    font-size: 14px;
  }
  :deep(.el-tabs__header) {
    margin: 0;
  }
}
.card-content {
  padding: 0 10px;
}
.chart {
  width: 100%;
  height: 220px;
}
:deep(.el-card__header) {
  padding: 10px;
}
</style>
